//
//  HMWelcomeViewController.swift
//  WB25
//
//  Created by HM on 2017/7/2.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit

class HMWelcomeViewController: UIViewController {
    
    
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        setupHeadImageViewAndMessageLabelAnim()
    }
    
    // 设置头像和文字动画
    private func setupHeadImageViewAndMessageLabelAnim(){
        
        
        self.headImageView.snp.updateConstraints({ (make) in
            make.top.equalTo(self.view).offset(100)
        })
        
        // 阻尼动画(是和frame相关)
        /*
            - 动画时间 
            - 延迟时间
            - 阻尼系数 0-1 越小弹性越好
            - 起始加速度
            - 动画选项
         */
        UIView.animate(withDuration: 1, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            // 强制刷新当前界面
            self.view.layoutIfNeeded()
        }) { (_) in
            // 设置文字动画
            UIView.animate(withDuration: 0.25, animations: { 
                
                self.messageLabel.alpha = 1
                
            }, completion: { (_) in
                // 发送通知 切换根控制器到HMMainVc
                NotificationCenter.default.post(name: HMSWITCHROOTVIEWCONTROLLERNOTI, object: "welcomeVC")
            })
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    // 设置视图
    private func setupUI(){
        // 添加控件
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        // 添加约束
        headImageView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp.bottom).offset(20)
        }
    }
    
    // 懒加载控件
    // 背景图片
    private lazy var bgImageView: UIImageView = UIImageView(named: "ad_background")
    // 个人头像
    private lazy var headImageView: UIImageView = {
        let img = UIImageView(named: "avatar_default_big")
        img.layer.cornerRadius = 45
        img.layer.borderColor = HMThemeColor.cgColor
        img.layer.borderWidth = 1
        img.clipsToBounds = true
        img.hm_setImage(withUrlString: HMOAuthViewModel.share.userAccountModel?.avatar_large)
        return img
    }()
    // 欢迎回来
    private lazy var messageLabel: UILabel = {
        let lab = UILabel(text: "欢迎回来", textColor: UIColor.darkGray, fontSize: HMBigFontSize)
        // 对齐方式
        lab.textAlignment = .center
        lab.alpha = 0
        return lab
    }()
}
